import fs from '@ohos.file.fs';
import { TuanjieLog } from '../common/TuanjieLog';

export class PlayerPrefs {
  public static directoryInt = null;
  public static directoryFloat = null;
  public static directoryString = null;

  public static jsonInt = "{}";
  public static jsonFloat = "{}";
  public static jsonString = "{}";

  private static fileSavePath = "";

  public static Init(filePathPrefix) {
    PlayerPrefs.fileSavePath = filePathPrefix;
    let filePath = filePathPrefix + "/PlayerPreference.txt";
    //Prevent crash
    try {
      let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE);
      fs.closeSync(file);

      let str = fs.readTextSync(filePath);
      if (str != "") {
        let arr = str.split('\n');
        PlayerPrefs.jsonInt = arr[0];
        PlayerPrefs.jsonFloat = arr[1];
        PlayerPrefs.jsonString = arr[2];
      }
    } catch (err) {
      TuanjieLog.error('fs openSync Failed. Error: %{public}s',
        JSON.stringify(err) ?? '');
    }

    if (PlayerPrefs.directoryInt == null) {
      PlayerPrefs.directoryInt = JSON.parse(PlayerPrefs.jsonInt);
    }
    if (PlayerPrefs.directoryFloat == null) {
      PlayerPrefs.directoryFloat = JSON.parse(PlayerPrefs.jsonFloat);
    }
    if (PlayerPrefs.directoryString == null) {
      PlayerPrefs.directoryString = JSON.parse(PlayerPrefs.jsonString);
    }
  }

  public static SetInt(name, value) {
    PlayerPrefs.directoryInt[name] = value;
  }

  public static SetFloat(name, value) {
    PlayerPrefs.directoryFloat[name] = value
  }

  public static SetString(name, value) {
    PlayerPrefs.directoryString[name] = value
  }

  public static GetInt(name, def) {
    if (PlayerPrefs.directoryInt[name] == null) {
      return def;
    }

    return PlayerPrefs.directoryInt[name];
  }

  public static GetFloat(name, def) {
    if (PlayerPrefs.directoryFloat[name] == null) {
      return def;
    }

    return PlayerPrefs.directoryFloat[name];
  }

  public static GetString(name, def) {
    if (PlayerPrefs.directoryString[name] == null) {
      return def;
    }

    return PlayerPrefs.directoryString[name];
  }

  public static HasKey(name) {
    if (PlayerPrefs.directoryString[name] != null) {
      return true;
    }
    if (PlayerPrefs.directoryFloat[name] != null) {
      return true;
    }
    if (PlayerPrefs.directoryInt[name] != null) {
      return true;
    }

    return false;
  }

  public static DeleteKey(name) {
    if (PlayerPrefs.directoryString[name] != null) {
      delete PlayerPrefs.directoryString[name]
    }
    if (PlayerPrefs.directoryFloat[name] != null) {
      delete PlayerPrefs.directoryFloat[name]
    }
    if (PlayerPrefs.directoryInt[name] != null) {
      delete PlayerPrefs.directoryInt[name]
    }
  }

  public static DeleteAll() {
    Object.keys(PlayerPrefs.directoryString).forEach(function (key) {
      delete PlayerPrefs.directoryString[key];
    });
    Object.keys(PlayerPrefs.directoryFloat).forEach(function (key) {
      delete PlayerPrefs.directoryFloat[key];
    });
    Object.keys(PlayerPrefs.directoryInt).forEach(function (key) {
      delete PlayerPrefs.directoryInt[key];
    });
  }

  public static Save(filePathPrefix) {
    PlayerPrefs.jsonInt = JSON.stringify(PlayerPrefs.directoryInt);
    PlayerPrefs.jsonFloat = JSON.stringify(PlayerPrefs.directoryFloat);
    PlayerPrefs.jsonString = JSON.stringify(PlayerPrefs.directoryString);

    let filePath = filePathPrefix + "/PlayerPreference.txt";
    try {
      let file = fs.openSync(filePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);
      let content = PlayerPrefs.jsonInt + '\n' + PlayerPrefs.jsonFloat + '\n' + PlayerPrefs.jsonString + '\n';
      fs.writeSync(file.fd, content);
      fs.closeSync(file);
    } catch (err) {
      TuanjieLog.error('fs openSync Failed. Error: %{public}s',
        JSON.stringify(err) ?? '');
    }
  }

  public static LocalSave() {
    PlayerPrefs.Save(PlayerPrefs.fileSavePath);
  }
}
